using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.ComponentModel;


namespace TaskLogger
{
    class TransparentPanel : Panel
    {
        public TransparentPanel()
        {
        }



        protected override void OnControlAdded(ControlEventArgs e)
        {
            base.OnControlAdded(e);
            RefreshRegion();
        }

        protected override void OnControlRemoved(ControlEventArgs e)
        {
            base.OnControlRemoved(e);
            RefreshRegion();
        }

        private void RefreshRegion()
        {
            if (!DesignMode)
            {
                GraphicsPath gp = new GraphicsPath();
                foreach (Control c in this.Controls)
                {
                    c.ClientSizeChanged += new EventHandler(c_ClientSizeChanged);
                    c.LocationChanged += new EventHandler(c_LocationChanged);
                    c.VisibleChanged += new EventHandler(c_VisibleChanged);
                    if (c.Visible)
                    {
                        Rectangle r = new Rectangle(c.Left, c.Top, c.ClientSize.Width, c.ClientSize.Height);
                        gp.AddRectangle(r);
                    } 
                }
                Region = new Region(gp);
            }

        }

        void c_VisibleChanged(object sender, EventArgs e)
        {
            RefreshRegion();
        }

        void c_LocationChanged(object sender, EventArgs e)
        {
            RefreshRegion();
        }

        void c_ClientSizeChanged(object sender, EventArgs e)
        {
            RefreshRegion();
        }


    }
}
